/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.classloader;

import picounit.features.Feature;
import picounit.features.base.ClassPath;
import picounit.features.base.FeatureRegistry;
import picounit.features.base.PicoUnitName;
import picounit.features.classloader.mutator.AggregateClassMutator;
import picounit.features.classloader.observer.AggregateClassObserver;
import picounit.features.test_finder.ObjectReloader;

public class PicoUnitClassLoaderFeature implements Feature {
	private final ClassPath classPath = new ClassPath();

	private final PicoUnitName picoUnitName;
	
	private final AggregateClassObserver aggregateClassObserver;
	private final AggregateClassMutator aggregateClassMutator;

	public PicoUnitClassLoaderFeature(PicoUnitName picoUnitName,
		AggregateClassObserver aggregateClassObserver, AggregateClassMutator aggregateClassMutator) {
			this.picoUnitName = picoUnitName;
			this.aggregateClassObserver = aggregateClassObserver;
			this.aggregateClassMutator = aggregateClassMutator;
	}

	public void addFeatures(FeatureRegistry featureRegistry) {
		PicoUnitClassLoader picoUnitClassLoader = new PicoUnitClassLoader(getClass().getClassLoader(),
			aggregateClassObserver.get(), aggregateClassMutator.get(), classPath, picoUnitName.picoUnitName());

		featureRegistry.addFeatures(picoUnitClassLoader);

		ClassReloader classReloader = new ClassReloader(picoUnitClassLoader);

		ObjectReloader objectReloader = new ObjectReloader(classReloader);

		featureRegistry.addFeatures(classReloader, objectReloader, new ImplementsClassTestFilterFactoryImpl(classReloader));
	}
}
